﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QldtSdh.BLL;
using QldtSdh.DAL.Models;
using QldtSdh.GUI.Common;
using QldtSdh.GUI.Utils;

namespace QldtSdh.GUI.DuLieu
{
    public partial class FormQuanLyDonVi : QldtSdh.GUI.Common.BaseDialog
    {

        public FormQuanLyDonVi()
        {
            InitializeComponent();
            gridData.AddRowIndexing();
            InitGrid();
            LoadGrid();
        }

        void InitGrid()
        {
            gridData.AutoGenerateColumns = false;
            colMaDonVi.DataPropertyName = "MaDonVi";
            colTenDonVi.DataPropertyName = "TenDonVi";
            colTruongDonVi.DataPropertyName = "TruongDonVi";
            colDienThoai.DataPropertyName = "SoDienThoai";
            colGhiChu.DataPropertyName = "GhiChu";
        }

        void LoadGrid()
        {
            bdData.DataSource = BizDonViDaoTao.GetList();
            gridData.DataSource = bdData;
        }

        private void bdData_CurrentChanged(object sender, EventArgs e)
        {
            var current = bdData.Current as DonVi;
            if (current != null)
            {
                btnSua.Enabled = btnXoa.Enabled = true;
            }
            else
            {
                btnSua.Enabled = btnXoa.Enabled = false;
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            var frm = new FormQuanLyDonVi_ChiTiet();
            frm.OnDonViListChanged += (oSender, oE) =>
                {
                    var addedDonVi = (DAL.Models.DonVi)(oE as EntityArgs).EntityObject;
                    LoadGrid();
                };
            frm.ShowDialog();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            var current = bdData.Current as DonVi;
            if (current != null)
            {
                var frm = new FormQuanLyDonVi_ChiTiet(current);
                if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    LoadGrid();
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            var current = bdData.Current as DonVi;
            if (current != null)
            {
                if (DialogUtils.ShowOKCancel(NOTIFY_CONSTANT_STRING.DELETE) == System.Windows.Forms.DialogResult.OK)
                {
                    var rs = BizDonViDaoTao.Delete(current.MaDonViGUID);
                    if (rs == CODE_RESULT_RETURN.ThanhCong)
                        bdData.RemoveCurrent();
                    else if (rs == CODE_RESULT_RETURN.ThatBai)
                        DialogUtils.ShowError(string.Format(ERROR_CONSTANT_STRING.LOI_XOA, ENTITY_NAME.DONVIDAOTAO));
                }
            }

        }

        private void FormQuanLyDonVi_FormClosing(object sender, FormClosingEventArgs e)
        {

        }

        private void btnSynchronize_Click(object sender, EventArgs e)
        {
            DialogUtils.ShowMessage("Đồng bộ dữ liệu Khoa Đào tạo với Đại học Huế");
        }
    }
}
